import { getDictItemsFromCache } from '@/api/api'
import Api from '@/api/sjsk/performanceServiceQuery'

const executeExamine = {
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 17 },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'assessSort',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '计划编号',
          align: "center",
          dataIndex: 'planNo'
        },
        {
          title: '计划名称',
          align: "center",
          dataIndex: 'planName',
        },
        {
          title: '考核类型',
          align: "center",
          dataIndex: 'assessType',
          customRender: (text, record, index) =>
            `${this.setTittle(record)}`

        },
        {
          title: '考核周期',
          align: "center",
          dataIndex: 'assessCycle'
        },
        {
          title: '下发时间',
          align: 'center',
          dataIndex: 'issuedTime'
        },
        {
          title: '考核状态',
          align: "center",
          dataIndex: 'assessStatus',
          customRender: (text, record, index) =>
            `${this.setStatus(record)}`
        },
        // {
        //   title: '签收状态',
        //   align: "center",
        //   dataIndex: 'assessStatus'
        // },
        {
          title: '初始化时间',
          align: 'center',
          dataIndex: 'initializationTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      examinType: getDictItemsFromCache('examinType'),
      examinStatus: getDictItemsFromCache('examinStatus'),
      month:getDictItemsFromCache('month'),
      quarter:getDictItemsFromCache('quarter'),
      yearShowOne:false,
      loading: false,
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam:{},
      pageData: {},
      checkList:[],
      componentName:'' // executeExam,selfExamine
    }
  },
  created(){
    this.componentName = this.$route.meta.componentName
    console.info('this.componentName',this.componentName)
    this.onQuery()
  },
  methods: {
    setTittle(val) {
      return this.$filterDictTextByCache('examinType', val.assessType) //现在字典中对应的文字，考核类型
    },
    setStatus(val){
      return this.$filterDictTextByCache('examinStatus', val.assessStatus) //现在字典中对应的文字，考核状态
    },
    // 查询
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    // 重置
    onResetForm() {
      this.queryParam = {}
      this.pageFilter = {
        pageNo: 1,
        pageSize: 10
      }
      this.refreshTable()
    },
    onSelectedRow(keys) {
      this.checkList = keys;
    },
    onTableChange(pagination, pageFilter, sorter) {
       let keyMap = { currentPage: 'pageNo' }
       let paginations = Object.keys(pagination).reduce((newData, key) => {
         let newKey = keyMap[key] || key
         newData[newKey] = pagination[key]
         return newData
       }, {})
       this.pageFilter = Object.assign({}, this.pageFilter, paginations)
       this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      let params = Object.assign({}, this.queryParam, this.pageFilter)
      let interfaceMethod = null;
      if(this.componentName === 'executeExam'){
        interfaceMethod = Api.implementSelfEvaluationList(params)
        // interfaceMethod = Api.getSelfEvaluationList(params)
      }else{
        interfaceMethod = Api.getSelfEvaluationList(params)
      }
      interfaceMethod.then((res) => {
          if (res.success) {
            this.pageData = res.result ? res.result : {}
            this.loading = false
            this.$refs.table.onClearRowKeys()
          } else {
            this.loading = false
            this.pageData = {}
            this.$message.warn('请求失败！')
          }
        }).catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    onDetail(record) {
      if(this.componentName === 'selfExamine'){
        this.$router.push({
          path: '/executeExamine/selfDetails',
          query: {
            id: record.id,
          },
        })
      }else{
        this.$router.push({
          path: '/executeExamine/executeExamineDetails',
          query: {
            id: record.id,
          },
        })
      }
    },
    // 弹出日历和关闭日历的回调
    openChangeOne(status) {
      if (status) {
        this.yearShowOne = true;
      }
    },
    // 得到年份选择器的值
    panelChangeOne(value) {
      this.yearShowOne = false;
      this.queryParam.year = value;
    },
    // 执行考核-按钮
    handleProups(){
      if(!this.checkList.length){
        return this.$message.warn('请先勾选对应的记录')
      }
      let that = this;
      this.$confirm({
        title: `是否确定对选中的记录进行自评吗?`,
        okText: "确认",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          that.loading = true;
          let ids = [];
          for(let el in that.checkList){
            ids.push(el.id)
          }
          // let params = Object.assign({}, that.queryParam, that.pageFilter)
          Api.selfEvaluation({id:ids.join(',')}).then((res) => {
            if (res.success) {
              that.loading = false
              that.$message.warn('执行考核成功！')
              that.refreshTable()
            } else {
              that.loading = false
              that.$message.warn('请求失败！')
            }
          }).catch((e) => {
            that.loading = false
            that.$message.warn('请求失败！')
          })
        }
      });
    }
  }
}
export default executeExamine